package com.example.stlink.configs.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.stlink.R;
import com.example.stlink.model.domain.CourseSign;
import com.example.stlink.utils.CommonUtils;

import java.util.List;

/**
 * 用于将课程数据渲染进ListView控件中进行显示
 */
public class StuClassListFragAdapter extends RecyclerView.Adapter<StuClassListFragAdapter.ViewHolder> implements View.OnClickListener  {

    private com.example.stlink.configs.onRecyclerViewItemClickListener  onRecyclerViewItemClickListener;
    private RecyclerView rvParent;

    private final List<CourseSign> mCourseSign;

    private final Context mContext;
    private final int resourceId;
    private int role;

    private Handler handler;

    public StuClassListFragAdapter(@NonNull Context context, int resourceId, @NonNull List<CourseSign> mCourseSign, int role) {
        this.mContext = context;
        this.mCourseSign = mCourseSign;
        this.resourceId = resourceId;
        this.role = role;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //指定一个父控件
        rvParent = (RecyclerView) parent;

        View view = LayoutInflater.from(mContext)
                .inflate(resourceId, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(this);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CourseSign courseSign = mCourseSign.get(position);
        System.out.println("mCourseSign[" + position + "]: " + courseSign);
        holder.tvCourseName.setText(courseSign.getCourseName());
        holder.tvSignAddress.setText(courseSign.getCourseAddr());
        Long signCreateTime = courseSign.getCreateTime();
        holder.tvCreateTime.setText(CommonUtils.stampToDate(String.valueOf(signCreateTime), "yyyy-MM-dd hh:mm:ss"));

    }

    @Override
    public int getItemCount() {
        return mCourseSign.size();
    }

    //重载点击事件，指定点击事件时执行自定义的onItemClick
    @Override
    public void onClick(View view) {
        int position = rvParent.getChildAdapterPosition(view);
        if (onRecyclerViewItemClickListener != null){
            onRecyclerViewItemClickListener.onItemClick(rvParent, view, position);
        }
    }


    //实例化自定义接口
    public void setOnItemClickListener(com.example.stlink.configs.onRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCourseName;
        TextView tvSignAddress;
        TextView tvCreateTime;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCourseName = itemView.findViewById(R.id.tv_class_list_title);
            tvSignAddress = itemView.findViewById(R.id.tv_sign_address);
            tvCreateTime = itemView.findViewById(R.id.tv_create_time);
        }
    }
}
